<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto Serif SC:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-bounce.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhiruozzy.cn","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":"truw","sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="这是我制作的第一个小游戏🙆‍♀️">
<meta property="og:type" content="article">
<meta property="og:title" content="2048小游戏">
<meta property="og:url" content="http://zhiruozzy.cn/2022/02/27/2048/index.html">
<meta property="og:site_name" content="芷若">
<meta property="og:description" content="这是我制作的第一个小游戏🙆‍♀️">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://zhiruozzy.cn/img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/2048-16462039259381.png">
<meta property="og:image" content="http://zhiruozzy.cn/img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/%E6%9B%B4%E6%96%B0%E8%A7%86%E5%9B%BE.png">
<meta property="og:image" content="http://zhiruozzy.cn/img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/%E6%9B%B4%E6%96%B0%E8%A7%86%E5%9B%BE(1).png">
<meta property="og:image" content="http://zhiruozzy.cn/img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/%E6%9B%B4%E6%96%B0%E8%A7%86%E5%9B%BE(2).png">
<meta property="article:published_time" content="2022-02-27T10:27:48.000Z">
<meta property="article:modified_time" content="2022-04-03T03:22:32.490Z">
<meta property="article:author" content="芷若">
<meta property="article:tag" content="游戏">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://zhiruozzy.cn/img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/2048-16462039259381.png">

<link rel="canonical" href="http://zhiruozzy.cn/2022/02/27/2048/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>2048小游戏 | 芷若</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">芷若</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-主页">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>主页</a>

  </li>
        <li class="menu-item menu-item-关于">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-标签">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-目录">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>目录</a>

  </li>
        <li class="menu-item menu-item-归档">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

      
    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/zhiruozzy" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://zhiruozzy.cn/2022/02/27/2048/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/touxiang.jpg">
      <meta itemprop="name" content="芷若">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="芷若">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          2048小游戏
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-02-27 18:27:48" itemprop="dateCreated datePublished" datetime="2022-02-27T18:27:48+08:00">2022-02-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-04-03 11:22:32" itemprop="dateModified" datetime="2022-04-03T11:22:32+08:00">2022-04-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%87%AA%E5%88%B6/" itemprop="url" rel="index"><span itemprop="name">自制</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>8.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>8 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>这是我制作的第一个小游戏🙆‍♀️</p>
<span id="more"></span>

<p>自学了HTML，CSS，和JS的一点皮毛🤦‍♀️</p>
<p>   只能说一个这个有些稚嫩的成果，基本啥都没有，因为JS基本美学，所以花了好久时间才勉强看懂代码的意思，全程也是跟着网上写好的代码敲出来的（当花了一天时间才看懂之后就不想自己敲了，开始摆烂），唯一的优点是它确实能跑，缺点是“能跑”的补集😅</p>
<p>​                                                                                         —-2022.02.28</p>
<p>   在学长验收的时候被揭了个老底，学长说我的代码和网上的一个特别像，可人家明明是照着敲的，明明是一摸一样（捂脸），谢谢学长给我留了点面子。这次验收也让我明白，不应该过度追求界面的美化，应该追求代码的逻辑和结构，学术诚信十分重要！会议结束后立马痛改前非，又开始了JS的自学之路，这次是真的自己一个一个字敲出来的，因为几天前刚学了贪吃蛇的写法，知道了游戏应该怎么写，JS的语法，所以这次写的超级顺畅，几个小时就自己捣鼓出来啦，还加上了我喜欢的蓝色。当学习到了一定程度再回看的时候，发现这个小游戏真的很好写🙇‍♀️</p>
<p>下面是我在理解网上的代码是总结出来的这个游戏的思路（和我写的不太一样，我把我的代码附在最后了）：</p>
<p>①变量：</p>
<ul>
<li>一个二维数组，负责记录当前位置的分数（对应就是屏幕上的16个块）</li>
<li>当前的分数</li>
<li>当前游戏的状态，运行（1）or已经结束（0）</li>
</ul>
<p>②成员函数</p>
<ul>
<li>游戏的初始化</li>
</ul>
<ol>
<li>分数初始化为0</li>
<li>对16个方格对应的值进行赋值为0</li>
<li>当前的游戏状态设为1（正在运行）</li>
<li>调用生成随机数函数两次，生成两个随机数</li>
<li>调用更新视图函数，</li>
</ol>
<ul>
<li>生成随机数</li>
</ul>
<ol>
<li>分别随机生成行和列</li>
<li>如果对应的元素为0的话，就说明这个位置是可以的</li>
<li>则在随机生成2或者4，这里用到了Math.random()，是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值，所以我们要以均等的概率生成2或4，代码就是<code>var num = Math.random()&gt;0.5?2:4;</code></li>
</ol>
<ul>
<li>更新当前界面（JS与HTML链接的地方）</li>
</ul>
<ol>
<li>两层for循环遍历data数组，通过getElementById来获取[i][j]位置上的方格,赋值给变量div</li>
<li>如果data[i][j]的值不为0 通过div.innerHTML把格子显示的数值改成data[i][j]（更新数据），通过div.className将格子代表的数值也进行更新</li>
<li>如果data[i][j]的值为0，格子内什么也不显示，对应设置div.innerHTML&#x3D;’’，div.className&#x3D;’grid’</li>
</ol>
<ul>
<li>更新屏幕上的分数</li>
</ul>
<p>通过getElementById来获得score01（当前分数）的标签，将其中的innerHTML改为当前的分数（this.score）</p>
<ul>
<li>检测游戏状态</li>
</ul>
<ol>
<li>如果this.status&#x3D;&#x3D;0(游戏结束）将this.score赋值给score02(结束时的分数）的innerHTML</li>
</ol>
<ul>
<li>判断游戏是否结束（没结束返回false，结束返回true）</li>
</ul>
<p>没有结束的三种情况：</p>
<ol>
<li>两层循环遍历data ，如果data[i][j]为零，返回false（① 数组中还有0）</li>
<li>在遍历中，如果j&lt;3并且data[i][j]等于data[i][j+1]，返回false（②左右相邻的还有相同的）</li>
<li>在遍历中，如果i&lt;3并且data[i][j]等于data[i+1][j]，返回false（③上下相邻的还有相同的）</li>
<li>如果遍历完都没有返回，说明游戏结束了，返回true</li>
</ol>
<ul>
<li>方格向左移动</li>
</ul>
<ol>
<li>用String函数将变动前的data二维数组转化成一个字符串before，以便变动之后与变动后的data数组比较。</li>
<li>遍历每一行，调用左移一行的函数</li>
<li>用String函数将变动后的data二维数组转化成一个字符串after，比较before和after</li>
<li>如果不相等，说明布局变化了，需要判断游戏是否结束，如果游戏结束，则将游戏状态status设为0，如果游戏没有结束，调用更新当前界面函数</li>
</ol>
<ul>
<li>左移一行的函数：</li>
</ul>
<ol>
<li><p>遍历当前行的所有列，得到当前元素[i][j]的右边不为0的第一个位置[i][nextj]</p>
</li>
<li><p>如果当前元素为0，左移之后直接会覆盖，所以就让当前元素等于data[i][nextj]，并把data[i][nextj]置0，让j–（要从当前位置继续遍历）</p>
</li>
<li><p>如果当前元素不为0且等于它右边不为0的第一个元素，则左移以后两个方块会合并为一个，并将data<em>2，所以this.data[i][j]</em>&#x3D;2；并将分数加上。</p>
</li>
</ol>
<p>下面是我做的时候写的思维导图，希望对你有帮助：</p>
<p><img src="/../img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/2048-16462039259381.png" alt="2048"></p>
<p>里面用到的函数：</p>
<ol>
<li>更新当前视图</li>
</ol>
<p><img src="/../img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/%E6%9B%B4%E6%96%B0%E8%A7%86%E5%9B%BE.png" alt="更新视图"></p>
<ol start="2">
<li><p>移动本行（这里以向左移动为例）</p>
<p><img src="/../img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/%E6%9B%B4%E6%96%B0%E8%A7%86%E5%9B%BE(1).png" alt="更新视图(1)"></p>
</li>
<li><p>生成随机位置</p>
</li>
</ol>
<p><img src="/../img/%E6%88%91%E5%81%9A%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%942048/%E6%9B%B4%E6%96%B0%E8%A7%86%E5%9B%BE(2).png" alt="更新视图(2)"></p>
<p>链接如下，要不点进去看看，哎喂看看我的配色也行啊！</p>
<p><a href="https://zhiruozzy.cn/Web/">2048小游戏</a></p>
<h3 id="下面是代码部分："><a href="#下面是代码部分：" class="headerlink" title="下面是代码部分："></a>下面是代码部分：</h3><h4 id="HTML部分："><a href="#HTML部分：" class="headerlink" title="HTML部分："></a>HTML部分：</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span> <span class="attr">id</span>=<span class="string">&quot;hh&quot;</span>&gt;</span>2048<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">span</span> <span class="attr">id</span>=<span class="string">&quot;score&quot;</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">h1</span> <span class="attr">id</span>=<span class="string">&quot;Score&quot;</span>&gt;</span>score:<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c00&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c01&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c02&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c03&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c10&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c11&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c12&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c13&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c20&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c21&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c22&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c23&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c30&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c31&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c32&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cell&quot;</span> <span class="attr">id</span>=<span class="string">&quot;c33&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="CSS部分"><a href="#CSS部分" class="headerlink" title="CSS部分"></a>CSS部分</h4><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">&lt;style&gt;</span><br><span class="line">        * &#123;</span><br><span class="line">            <span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line">            <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="selector-id">#hh</span> &#123;</span><br><span class="line">            <span class="attribute">text-align</span>: center;</span><br><span class="line">            <span class="attribute">color</span>: <span class="number">#4dabf7</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="selector-id">#score</span> &#123;</span><br><span class="line">            <span class="attribute">text-align</span>: center;</span><br><span class="line">            <span class="attribute">color</span>: <span class="number">#4263eb</span>;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="selector-id">#box</span> &#123;</span><br><span class="line">            <span class="attribute">width</span>: <span class="number">320px</span>;</span><br><span class="line">            <span class="attribute">height</span>: <span class="number">320px</span>;</span><br><span class="line">            <span class="attribute">border</span>: <span class="number">2px</span> solid;</span><br><span class="line">            <span class="attribute">border-color</span>: <span class="built_in">rgb</span>(<span class="number">78</span>, <span class="number">167</span>, <span class="number">235</span>);</span><br><span class="line">            <span class="attribute">border-radius</span>: <span class="number">25px</span>;</span><br><span class="line">            <span class="attribute">margin</span>: <span class="number">40px</span> auto;</span><br><span class="line">            <span class="attribute">background-color</span>: <span class="number">#a5d8ff</span>;</span><br><span class="line">            <span class="attribute">box-shadow</span>: <span class="number">4px</span> <span class="number">4px</span> <span class="number">3px</span> <span class="built_in">rgba</span>(<span class="number">35</span>, <span class="number">153</span>, <span class="number">243</span>, <span class="number">0.9</span>)<span class="comment">/*blur值为10px*/</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="selector-class">.cell</span> &#123;</span><br><span class="line">            <span class="attribute">width</span>: <span class="number">76px</span>;</span><br><span class="line">            <span class="attribute">height</span>: <span class="number">76px</span>;</span><br><span class="line">            <span class="attribute">border</span>: <span class="number">2px</span> solid;</span><br><span class="line">            <span class="attribute">border-radius</span>: <span class="number">25px</span>;</span><br><span class="line">            <span class="attribute">border-color</span>: <span class="number">#87CEFA</span>;</span><br><span class="line">            <span class="attribute">background-color</span>: <span class="number">#b2e1ff</span>;</span><br><span class="line">            <span class="attribute">float</span>: left;</span><br><span class="line">            <span class="attribute">font-size</span>: <span class="number">30px</span>;</span><br><span class="line">            <span class="attribute">font-weight</span>: <span class="number">500</span>;</span><br><span class="line">            <span class="comment">/* 文字居中 */</span></span><br><span class="line">            <span class="attribute">text-align</span>: center;</span><br><span class="line">            <span class="attribute">line-height</span>: <span class="number">76px</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="selector-tag">body</span> &#123;</span><br><span class="line">            <span class="attribute">background</span>: <span class="built_in">url</span>(<span class="string">https://pic.imgdb.cn/item/623885a627f86abb2abd3bf3.jpg</span>) ;</span><br><span class="line">            <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line">            <span class="attribute">background-repeat</span>:no-repeat;</span><br><span class="line">            <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">            <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">            <span class="attribute">background-attachment</span>: fixed;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &lt;/style&gt;</span><br></pre></td></tr></table></figure>

<h4 id="JS部分（核心）："><a href="#JS部分（核心）：" class="headerlink" title="JS部分（核心）："></a>JS部分（核心）：</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">       <span class="keyword">var</span> score = <span class="number">0</span>;</span><br><span class="line">       <span class="keyword">var</span> i = <span class="number">0</span>, j = <span class="number">0</span>;</span><br><span class="line">       <span class="keyword">var</span> span = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;score&quot;</span>);</span><br><span class="line">       <span class="keyword">var</span> arr = [[<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]];</span><br><span class="line">       <span class="title function_">creat</span>();</span><br><span class="line">       <span class="title function_">creat</span>();</span><br><span class="line">       <span class="title function_">update</span>();</span><br><span class="line"></span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">later</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">var</span> ret = <span class="title function_">judge</span>();</span><br><span class="line">           <span class="variable language_">console</span>.<span class="title function_">log</span>(ret);</span><br><span class="line">           <span class="keyword">if</span> (ret == <span class="literal">false</span>) &#123;</span><br><span class="line">               <span class="title function_">alert</span>(<span class="string">&quot;游戏结束,你的分数是:&quot;</span>+score);</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="title function_">update</span>();</span><br><span class="line">           <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">check</span>());</span><br><span class="line">           <span class="comment">//如果有空位才create一个新的</span></span><br><span class="line">           <span class="keyword">if</span> (<span class="title function_">check</span>()) &#123;</span><br><span class="line">               <span class="title function_">creat</span>();</span><br><span class="line">               <span class="title function_">update</span>();</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">check</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">                   <span class="keyword">if</span> (arr[i][j] == <span class="number">0</span>)</span><br><span class="line">                       <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">creat</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">               <span class="keyword">var</span> px = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">4</span>);</span><br><span class="line">               <span class="keyword">var</span> py = <span class="built_in">parseInt</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">4</span>);</span><br><span class="line">               <span class="keyword">if</span> (arr[px][py] == <span class="number">0</span>) &#123;</span><br><span class="line">                   arr[px][py] = <span class="title class_">Math</span>.<span class="title function_">random</span>() &lt; <span class="number">0.5</span> ? <span class="number">2</span> : <span class="number">4</span>;</span><br><span class="line">                   <span class="keyword">break</span>;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       <span class="comment">//两种没有输的情况：1.还有空位 2.相邻的元素相等</span></span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">judge</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">                   <span class="keyword">if</span> (arr[i][j] == <span class="number">0</span>)</span><br><span class="line">                       <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                   <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] &amp;&amp; j &lt; <span class="number">3</span> &amp;&amp; arr[i][j] == arr[i][j + <span class="number">1</span>])</span><br><span class="line">                       <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                   <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] &amp;&amp; i &lt; <span class="number">3</span> &amp;&amp; arr[i][j] == arr[i + <span class="number">1</span>][j])</span><br><span class="line">                       <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">update</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="comment">//更新div中的innerHTML</span></span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">                   <span class="keyword">if</span> (arr[i][j] == <span class="number">0</span>) &#123;</span><br><span class="line">                       <span class="keyword">var</span> cell = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;c&quot;</span> + i + j);</span><br><span class="line">                       cell.<span class="property">innerHTML</span> = <span class="string">&quot;&quot;</span>;</span><br><span class="line">                       cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#b2e1ff&quot;</span>;</span><br><span class="line">                   &#125;</span><br><span class="line">                   <span class="keyword">else</span> &#123;</span><br><span class="line">                       <span class="keyword">var</span> cell = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;c&quot;</span> + i + j);</span><br><span class="line">                       cell.<span class="property">innerHTML</span> = arr[i][j];</span><br><span class="line">                       <span class="keyword">if</span> (arr[i][j] == <span class="number">2</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#e7f5ff&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">4</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#d0ebff&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">8</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#82c9ff&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">16</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#74c0fc&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">32</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#4dabf7&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">64</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#339af0&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">128</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#228be6&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">256</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#1c7ed6&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">512</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#fff0f6&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">1024</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#1971c2&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">2048</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#1864ab&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (arr[i][j] == <span class="number">4096</span>) &#123;</span><br><span class="line">                           cell.<span class="property">style</span>.<span class="property">backgroundColor</span> = <span class="string">&quot;#4263eb&quot;</span>;</span><br><span class="line">                       &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="comment">//更新分数</span></span><br><span class="line">           span.<span class="property">innerHTML</span> = <span class="string">&quot;&lt;h1&gt;score:&quot;</span> + score + <span class="string">&quot;&lt;/h3&gt;&quot;</span>;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">moveLeft</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++) &#123;</span><br><span class="line">                   <span class="keyword">for</span> (<span class="keyword">var</span> k = j + <span class="number">1</span>; k &lt; <span class="number">4</span>; k++) &#123;</span><br><span class="line">                       <span class="keyword">if</span> (arr[i][j] &amp;&amp; arr[i][j] == arr[i][k]) &#123;</span><br><span class="line">                           arr[i][j] *= <span class="number">2</span>;</span><br><span class="line">                           arr[i][k] = <span class="number">0</span>;</span><br><span class="line">                           score += arr[i][j];</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (!arr[i][j] &amp;&amp; arr[i][k]) &#123;</span><br><span class="line">                           arr[i][j] = arr[i][k];</span><br><span class="line">                           arr[i][k] = <span class="number">0</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">moveRight</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">3</span>; j &gt; <span class="number">0</span>; j--) &#123;</span><br><span class="line">                   <span class="keyword">for</span> (<span class="keyword">var</span> k = j - <span class="number">1</span>; k &gt;= <span class="number">0</span>; k--) &#123;</span><br><span class="line">                       <span class="keyword">if</span> (arr[i][j] &amp;&amp; arr[i][j] == arr[i][k]) &#123;</span><br><span class="line">                           arr[i][j] *= <span class="number">2</span>;</span><br><span class="line">                           arr[i][k] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">                           score += arr[i][j];</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (!arr[i][j] &amp;&amp; arr[i][k]) &#123;</span><br><span class="line">                           arr[i][j] = arr[i][k];</span><br><span class="line">                           arr[i][k] = <span class="number">0</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">moveUp</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">                   <span class="keyword">for</span> (<span class="keyword">var</span> k = i + <span class="number">1</span>; k &lt; <span class="number">4</span>; k++) &#123;</span><br><span class="line">                       <span class="keyword">if</span> (arr[i][j] &amp;&amp; arr[i][j] == arr[k][j]) &#123;</span><br><span class="line">                           arr[i][j] *= <span class="number">2</span>;</span><br><span class="line">                           arr[k][j] = <span class="number">0</span>;</span><br><span class="line">                           score += arr[i][j];</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (!arr[i][j] &amp;&amp; arr[k][j]) &#123;</span><br><span class="line">                           arr[i][j] = arr[k][j];</span><br><span class="line">                           arr[k][j] = <span class="number">0</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">moveDown</span>(<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;</span><br><span class="line">               <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">3</span>; i &gt; <span class="number">0</span>; i--) &#123;</span><br><span class="line">                   <span class="keyword">for</span> (<span class="keyword">var</span> k = i - <span class="number">1</span>; k &gt;= <span class="number">0</span>; k--) &#123;</span><br><span class="line">                       <span class="keyword">if</span> (arr[i][j] &amp;&amp; arr[i][j] == arr[k][j]) &#123;</span><br><span class="line">                           arr[i][j] *= <span class="number">2</span>;</span><br><span class="line">                           arr[k][j] = <span class="number">0</span>;</span><br><span class="line">                           score += arr[i][j];</span><br><span class="line">                       &#125;</span><br><span class="line">                       <span class="keyword">else</span> <span class="keyword">if</span> (!arr[i][j] &amp;&amp; arr[k][j]) &#123;</span><br><span class="line">                           arr[i][j] = arr[k][j];</span><br><span class="line">                           arr[k][j] = <span class="number">0</span>;</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">       <span class="variable language_">document</span>.<span class="property">onkeydown</span> = <span class="keyword">function</span> (<span class="params">event</span>) &#123;</span><br><span class="line">           event = event || <span class="variable language_">window</span>.<span class="property">event</span>;</span><br><span class="line">           <span class="keyword">switch</span> (event.<span class="property">keyCode</span>) &#123;</span><br><span class="line">               <span class="comment">//左</span></span><br><span class="line">               <span class="keyword">case</span> <span class="number">37</span>:</span><br><span class="line">                   <span class="title function_">moveLeft</span>();</span><br><span class="line">                   <span class="title function_">later</span>();</span><br><span class="line">                   <span class="keyword">break</span>;</span><br><span class="line">               <span class="keyword">case</span> <span class="number">38</span>:</span><br><span class="line">                   <span class="title function_">moveUp</span>();</span><br><span class="line">                   <span class="title function_">later</span>();</span><br><span class="line">                   <span class="keyword">break</span>;</span><br><span class="line">               <span class="keyword">case</span> <span class="number">39</span>:</span><br><span class="line">                   <span class="title function_">moveRight</span>();</span><br><span class="line">                   <span class="title function_">later</span>();</span><br><span class="line">                   <span class="keyword">break</span>;</span><br><span class="line">               <span class="keyword">case</span> <span class="number">40</span>:</span><br><span class="line">                   <span class="title function_">moveDown</span>();</span><br><span class="line">                   <span class="title function_">later</span>();</span><br><span class="line">                   <span class="keyword">break</span>;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">   &lt;/script&gt;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%B8%B8%E6%88%8F/" rel="tag"># 游戏</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/02/26/%E4%B9%9F%E8%8C%AB%E7%84%B6-%E8%BF%98%E5%A5%BD%E6%9C%89%E5%8B%87%E6%95%A2/" rel="prev" title="也茫然 还好有勇敢">
      <i class="fa fa-chevron-left"></i> 也茫然 还好有勇敢
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/02/28/%E4%B8%80%E4%BA%9B%E5%A5%BD%E7%94%A8%E7%9A%84%E7%BD%91%E7%AB%99/" rel="next" title="一些好用的网站">
      一些好用的网站 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8B%E9%9D%A2%E6%98%AF%E4%BB%A3%E7%A0%81%E9%83%A8%E5%88%86%EF%BC%9A"><span class="nav-number">1.</span> <span class="nav-text">下面是代码部分：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#HTML%E9%83%A8%E5%88%86%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">HTML部分：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#CSS%E9%83%A8%E5%88%86"><span class="nav-number">1.2.</span> <span class="nav-text">CSS部分</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#JS%E9%83%A8%E5%88%86%EF%BC%88%E6%A0%B8%E5%BF%83%EF%BC%89%EF%BC%9A"><span class="nav-number">1.3.</span> <span class="nav-text">JS部分（核心）：</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="芷若"
      src="/images/touxiang.jpg">
  <p class="site-author-name" itemprop="name">芷若</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhiruo" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhiruo" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">陕ICP备2022001874号 </a>
      <img src="http://www.beian.gov.cn/portal/download" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="芷若"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">芷若</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">105k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:35</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="//cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script>
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script>
<script src="/js/algolia-search.js"></script>














  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/assets/koharu.model.json"},"display":{"position":"right","width":145,"height":315},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
